package com.DataProcess;

/**
 * Created by Administrator on 2016/8/25 0025.
 */
public class ClientData {
    private String thisLogTag = "ClientData";

    public String type = ClientDataType.FAIL;

    public boolean isDebug = false;
    public boolean isEnable = false;
    public boolean isAutomatic = false;
    public boolean isReturn = false;
    public boolean isCloseLoop = true;
    public boolean isRefresh = false;

    private byte setCenter = 0;
    private byte setDir = 0;
    private double settingRange = 100.0;  //将发过来的控制指令归一化
    public double targetSpeedL = 0;
    public double targetSpeedR = 0;

    public short[] PID = {0,0,0};
    public int optimizeK = 10;
    public double optimizeValue = 0;

    public void decode(byte[] data) {
        if(data[0] < 0x50 || data[0] > 0x60)
            return;
        switch (data[0]) {
            case 0x50:
                flagDecode(data);
                break;
            case 0x52:
                controlDecode(data);
                break;
            case 0x57:
                PIDDecode(data);
                break;
            case 0x5a:
                optimizeDecode(data);
                break;
            default:
                type = ClientDataType.FAIL;
        }
    }

    void flagDecode(byte[] data){
        if (data[1] > 0) isDebug = true;
        else isDebug = false;

        if (data[2] > 0) isEnable = true;
        else isEnable = false;

        if (data[3] > 0) isAutomatic = true;
        else isAutomatic = false;

        if (data[4] > 0) isReturn = true;
        else isReturn = false;

        if (data[5] > 0) isCloseLoop = true;
        else isCloseLoop = false;

        if (data[6] > 0) isRefresh = true;
        else isRefresh = false;

        type = ClientDataType.FLAG;
    }

    void controlDecode(byte[] data){
        setCenter = data[1];
        setDir = data[2];
        targetSpeedL = (setCenter + setDir/2.0) /settingRange ;
        targetSpeedR = (setCenter - setDir/2.0) /settingRange ;

        type = ClientDataType.CONTROL;
    }

    void PIDDecode(byte[] data) {
        PID[0] = (short)(( (data[1]&0x00ff) << 8) |  (data[2]&0x00ff) );
        PID[1] = (short)(( (data[3]&0x00ff) << 8) |  (data[4]&0x00ff) );
        PID[2] = (short)(( (data[5]&0x00ff) << 8) |  (data[6]&0x00ff) );

        type = ClientDataType.PID;
    }
    void optimizeDecode(byte[] data) {
        optimizeK = (short)(( (data[1]&0x00ff) << 8) |  (data[2]&0x00ff) );
        optimizeValue = (double)(( (data[3]&0x00ff) << 8) |  (data[4]&0x00ff) )/100.0;

        type = ClientDataType.OPTIMIZE;
    }
}
